package com.ruoyi.familyManagement.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;

import com.ruoyi.familyManagement.domain.vo.FamilyInfoVO;
import com.ruoyi.familyManagement.service.IDocFamilyAddressService;
import com.ruoyi.familyManagement.domain.China;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "地址管理")
@RequestMapping("/FamilyAddress/docAddress")
public class DocFamilyAddressController {

    @Autowired
    private IDocFamilyAddressService addressService;

    /**
     * 获取所有省份信息
     *
     * @return ResponseEntity<List < China>> 包含所有省份信息的响应实体，其中body为China对象列表
     */
    @GetMapping("/getAllProvince")
    @ApiOperation("获取所有省份")
    public ResponseEntity<List<China>> getAllProvince() {
        // 调用地址服务获取所有省份数据
        List<China> list = addressService.getAllProvince();
        return ResponseEntity.ok(list);
    }

    /**
     * 根据省份id，查询城市
     *
     * @param provinceId 省份id
     * @return ResponseEntity<List < China>> 城市列表
     */
    @GetMapping("/getAllCities")
    @ApiOperation("根据省份id，查询城市")
    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, dataTypeClass = Integer.class)
    public ResponseEntity<List<China>> getAllCities(Integer provinceId) {
        // 调用服务获取城市列表
        List<China> list = addressService.getAllCities(provinceId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据城市id查询区/县信息
     *
     * @param cityId 城市id，用于查询该城市下的所有区/县
     * @return 返回区/县信息列表的响应实体
     */
    @GetMapping("/getAllDistricts")
    @ApiOperation("根据城市id，查询区/县")
    @ApiImplicitParam(name = "cityId", value = "城市id", required = true, dataTypeClass = Integer.class)
    public ResponseEntity<List<China>> getAllDistricts(Integer cityId) {
        // 调用地址服务获取区/县数据
        List<China> list = addressService.getAllDistricts(cityId);
        return ResponseEntity.ok(list);
    }

    /**
     * 更新家庭地址信息
     *
     * @param familyInfoVO 家庭信息
     * @return 更新结果
     */
    @ApiOperation("更新家庭地址")
    @PostMapping("/updateFamilyInfo")
    public AjaxResult updateFamilyInfo(@RequestBody FamilyInfoVO familyInfoVO) {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 调用服务更新家庭地址
        boolean result = addressService.updateFamilyInfo(userId, familyInfoVO);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

}
